package mmoon.mmoonstory.eventhandler;

import mmoon.mmoonstory.MMoonAll;
import mmoon.mmoonstory.MMoonStory;
import mmoon.mmoonstory.MMoonUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod.EventBusSubscriber(modid = MMoonStory.MOD_ID)
public class EventPlayer {

    @SubscribeEvent
    public static void onEntityItemPickup(EntityItemPickupEvent event) {
        // 如果玩家骑在别的实体上，就阻止他拾取物品
        if (event.getEntityPlayer().isRiding()) {
            event.setCanceled(true);
        }
    }

    @SubscribeEvent
    public static void atHarvestCheck(PlayerEvent.HarvestCheck event) {
        EntityPlayer player = event.getEntityPlayer();
        ItemStack holdItem = player.getHeldItem(EnumHand.MAIN_HAND);
        if (holdItem.isEmpty()) {
            player.attackEntityFrom(MMoonUtils.Scratch, Float.MIN_NORMAL);
        }
    }

    @SubscribeEvent
    public static void atBreakSpeed(PlayerEvent.BreakSpeed event) {
        if (event.getEntityPlayer().getItemStackFromSlot(EntityEquipmentSlot.CHEST).getItem() == MMoonAll.OBSIDIAN_CHESTPLATE) {
            event.setNewSpeed(event.getOriginalSpeed() / 2);
        }
    }
}
